package file

import (
	"context"
	v1 "devops-super/api/file/v1"
	"devops-super/internal/consts"
	"devops-super/internal/service"
	"github.com/gogf/gf/v2/frame/g"
)

// 通用上传
func (c *ControllerV1) CommonUpload(ctx context.Context, req *v1.FileCommonUploadReq) (res *v1.FileCommonUploadRes, err error) {
	res, err = service.File().CommonUpload(ctx, req)
	return
}

// 通用上传base64图片
func (c *ControllerV1) CommonBase64Upload(ctx context.Context, req *v1.FileCommonUploadBase64Req) (res *v1.FileCommonUploadBase64Res, err error) {
	upRes, err := service.File().CommonBase64Upload(ctx, req.Base64)
	if err != nil {
		return nil, err
	}
	res = &v1.FileCommonUploadBase64Res{
		Url: upRes,
	}
	return
}

// 上传单图
func (c *ControllerV1) SingleImg(ctx context.Context, req *v1.UploadSingleImgReq) (res *v1.UploadSingleRes, err error) {
	file := req.File
	v, _ := g.Cfg().Get(ctx, "upload.default")
	response, err := service.File().UploadFile(ctx, file, consts.CheckFileTypeImg, v.Int())
	if err != nil {
		return
	}
	res = &v1.UploadSingleRes{
		UploadResponse: response,
	}
	// 上传第三方
	return
}

// 上传多图
func (c *ControllerV1) MultipleImg(ctx context.Context, req *v1.UploadMultipleImgReq) (res *v1.UploadMultipleRes, err error) {
	files := req.File
	v, _ := g.Cfg().Get(ctx, "upload.default")
	mf, err := service.File().UploadFiles(ctx, files, consts.CheckFileTypeImg, v.Int())
	if err != nil {
		return
	}
	res = &mf
	return
}

// 上传单文件
func (c *ControllerV1) SingleFile(ctx context.Context, req *v1.UploadSingleFileReq) (res *v1.UploadSingleRes, err error) {
	file := req.File
	v, _ := g.Cfg().Get(ctx, "upload.default")
	response, err := service.File().UploadFile(ctx, file, consts.CheckFileTypeFile, v.Int())
	if err != nil {
		return
	}
	res = &v1.UploadSingleRes{
		UploadResponse: response,
	}
	return
}

// 上传多文件
func (c *ControllerV1) MultipleFile(ctx context.Context, req *v1.UploadMultipleFileReq) (res *v1.UploadMultipleRes, err error) {
	files := req.File
	v, _ := g.Cfg().Get(ctx, "upload.default")
	mf, err := service.File().UploadFiles(ctx, files, consts.CheckFileTypeFile, v.Int())
	if err != nil {
		return
	}
	res = &mf
	return
}
